#include "animatedbutton.h"

AnimatedButton::AnimatedButton(QWidget *parent) :
    QPushButton(parent)
{    
    setDefaultStyleSheet();

    m_animation = new QPropertyAnimation(this, "animationStep");
    m_animation->setDuration(300);
    m_animation->setStartValue(0);
    m_animation->setEndValue(ANIMATIONSTEPS);
}

int AnimatedButton::animationStep()
{
    return m_animationStep;
}

void AnimatedButton::setAnimationStep(int step)
{
    m_animationStep = step;

    int borderRed = 14 + ((double)m_animationStep * ((double)(255 - 14) / (double)(ANIMATIONSTEPS)));
    int borderGreen = 109 + ((double)m_animationStep * ((double)(255 - 109) / (double)(ANIMATIONSTEPS)));
    int borderBlue = 56 + ((double)m_animationStep * ((double)(255 - 56) / (double)(ANIMATIONSTEPS)));

    int backgroundRed = 14 + ((double)m_animationStep * ((double)(15 - 14) / (double)(ANIMATIONSTEPS)));
    int backgroundGreen = 109 + ((double)m_animationStep * ((double)(160 - 109) / (double)(ANIMATIONSTEPS)));
    int backgroundBlue = 56 + ((double)m_animationStep * ((double)(80 - 56) / (double)(ANIMATIONSTEPS)));

    this->setStyleSheet((QString)"QPushButton {" +
                        "font-family: \"Segoe UI\";" +
                        "color: white;" +
                        "border: 1px solid rgb(" + QString::number(borderRed) + ", " +
                        QString::number(borderGreen) + ", " + QString::number(borderBlue) + ");" +
                        "background-color: rgb(" + QString::number(backgroundRed) + ", " +
                        QString::number(backgroundGreen) + ", " + QString::number(backgroundBlue) + ");" +
                        "padding: 6px;" +
                        "}");
}

AnimatedButton::~AnimatedButton()
{
    delete m_animation;
}

void AnimatedButton::enterEvent(QEvent *)
{
    m_animation->start();
}

void AnimatedButton::leaveEvent(QEvent *)
{
    m_animation->stop();
    setDefaultStyleSheet();
}

void AnimatedButton::setDefaultStyleSheet()
{
    this->setStyleSheet((QString)"QPushButton {" +
                        "font-family: \"Segoe UI\";" +
                        "color: white;" +
                        "border: 1px solid rgb(14, 109, 56);" +
                        "background-color: rgb(14, 109, 56);" +
                        "padding: 6px;" +
                        "}");
}
